﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EfficientlyLazy.PhotoFramer.Entity;
using EfficientlyLazy.PhotoFramer.Service;

namespace EfficientlyLazy.PhotoFramer
{
    public partial class frmSettings : Form
    {
        private readonly ISettingService _settingService = ServiceFactory.Create<ISettingService>();

        private readonly SystemSettings _systemSettings;

        public frmSettings()
        {
            InitializeComponent();

            _systemSettings = _settingService.LoadSystemSettings();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PopulateSettingsForm();
        }

        private void PopulateSettingsForm()
        {
            propertyGrid1.SelectedObject = _systemSettings;

            cmdRevert.Enabled = false;
        }

        private void cmdSave_Click(object sender, EventArgs e)
        {
            if (_systemSettings.HasChanges())
            {
                var changes = _systemSettings.GetChangeMessage();

                var builder = new StringBuilder();

                builder.AppendLine("The following changes were made:");
                builder.AppendLine();
                builder.AppendLine(changes);
                builder.AppendLine();
                builder.AppendLine("Are you SURE you want to save?");

                if (MessageBox.Show(builder.ToString(), "Verify Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    return;
                }

                _settingService.SaveSystemSettings(_systemSettings);
            }
            else
            {
                MessageBox.Show("No Changes Made", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            Close();
        }

        private void cmdRevert_Click(object sender, EventArgs e)
        {
            _systemSettings.Revert();

            PopulateSettingsForm();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            cmdRevert.Enabled = true;
        }
    }
}
